﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MapManager : MonoBehaviour
{
    public GameObject[] OutWallArray;
    public GameObject[] FloorArray;
    public GameObject[] WallArray;
    public GameObject[] FoodArray;
    public GameObject[] EnemyArray;
    public GameObject exitPrefab;
    private GameManager gameManager;
    private int rows = 10;
    private int cols = 10;
    private Transform _mapHolder;
    private List<Vector2> positionList = new List<Vector2>();
    // Use this for initialization
    void Awake()
    {
        gameManager = new GameManager();
        InitMap();
    }

    // Update is called once per frame
    void Update()
    {

    }
    //初始化地图
    private void InitMap()
    {
        _mapHolder = new GameObject("Map").transform;
        //创建围墙
        for (int x = 0; x < cols; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                if (x == 0 || y == 0 || x == cols - 1 || y == rows - 1)
                {
                    int index = Random.Range(0, OutWallArray.Length);
                    GameObject goO = GameObject.Instantiate(OutWallArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    goO.transform.SetParent(_mapHolder);
                }
                else
                {
                    int index = Random.Range(0, FloorArray.Length);
                    GameObject goF = GameObject.Instantiate(FloorArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    goF.transform.SetParent(_mapHolder);
                }
            }
        }
        positionList.Clear();
        for (int x = 2; x < cols - 2; x++)
        {
            for (int y = 2; y < rows - 2; y++)
            {
                positionList.Add(new Vector2(x, y));
            }
        }

        //创建障碍物
        int wallCount = Random.Range(2, 9);//障碍物的个数
        InitItems(wallCount, WallArray);

        //创建食物 食物个数为 1 - level * 2
        int foodCount = Random.Range(1, gameManager.level + 1);
        InitItems(foodCount, FoodArray);

        //创建敌人 个数为 level/2 + 1
        int enemyCount = gameManager.level / 2 + 1;
        InitItems(enemyCount, EnemyArray);

        //创建出口
        GameObject goE = Instantiate(exitPrefab, new Vector2(cols - 2, rows - 2), Quaternion.identity) as GameObject;
        goE.transform.SetParent(_mapHolder);

    }

    private void InitItems(int count, GameObject[] prefabs)
    {
        for (int i = 0; i < count; i++)
        {
            GetPrefab(prefabs);
        }
    }

    private void GetPrefab(GameObject[] prefabs)
    {
        int index = Random.Range(0, prefabs.Length);
        GameObject go = GameObject.Instantiate(prefabs[index], RandomPosition(), Quaternion.identity) as GameObject;
        go.transform.SetParent(_mapHolder);
    }

    private Vector2 RandomPosition()
    {
        int positionIndex = Random.Range(0, positionList.Count);
        Vector2 pos = positionList[positionIndex];
        positionList.RemoveAt(positionIndex);
        return pos;
    }


}
